package com.app.business.service.uni;

import com.app.business.model.House;
import com.app.business.model.BxHouse;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 意向房源list
 */
@Service
@SuppressWarnings("all")
public class AppBxHouseService extends JpaBaseDao<BxHouse,Integer> {

    /**
     * 预选房源面积
     * @param contractId
     * @return
     */
    public BigDecimal getYxHouseArea(Integer contractId) {
        StringBuffer sql = new StringBuffer(" SELECT SUM(IFNULL(h.property_area,0) + IFNULL(h.pool_area,0)) AS yxHouseArea ");
        sql.append(" FROM bx_house bx,house h  ");
        sql.append(" WHERE bx.house_id = h.id AND bx.contract_id = :contractId ");
        Map<String, Object> map = new HashMap<>();
        map.put("contractId", contractId);
        return BigDecimalUtils.stringToBigDecimal(String.valueOf(this.findSqlObject(sql.toString(), map).get(0)));
    }


    /**
     * 得到contractId对应下的houseidList
     * @param contractId
     * @return
     */
    public List<Integer> getHouseIdByContractId(Integer contractId) {
        Map<String, Object> map = Maps.newLinkedHashMap();
        StringBuffer sql = new StringBuffer("select bx.house_id ");
        sql.append("from bx_house bx  where bx.contract_id = :contractId  ");
        map.put("contractId", contractId);
        return (List<Integer>) this.findSqlObject(sql.toString(), map);
    }

    /**
     * 删除预选方案
     * @param houseId
     * @param contractId
     */
    @Transactional
    public synchronized void deleteBxHouse(Integer houseId,Integer contractId) {
        String sql = "delete from bx_house  where house_id =:houseId and contract_id =:contractId";
        this.entityManager.createNativeQuery(sql).setParameter("houseId", houseId).setParameter("contractId",contractId).executeUpdate();
    }

    /**
     * 预选houseList
     * @param contractId
     * @return
     */
    public List<House> getYxHouseList(Integer contractId) {
        StringBuffer hql = new StringBuffer(" from House h  where h.id ");
        hql.append(" in (select yx.houseId from YxHouse yx where yx.contractId =:contractId)");
        List<House> houseList = entityManager.createQuery(hql.toString()).setParameter("contractId", contractId).getResultList();

        return houseList;
    }



}
